<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
    <th:block th:include="include :: header('人员角色授权列表')"/>
    <style>
        .layui-card-body .layui-form {
            margin: auto;
        }
        .layui-form-item {
            margin-bottom: 0px;
        }

    </style>
</head>
<body>
<div class="layuimini-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form id="formId" class="layui-form layui-form-pane">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">部门</label>
                    <div class="layui-input-inline">
                        <ul data-id="0" id="selectDept" name="deptId"></ul>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">角色</label>
                    <div class="layui-input-inline">
                        <input type="text" id="roleName" name="roleName" placeholder="点击选择角色"
                               class="layui-input" />
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">用户</label>
                    <div class="layui-input-inline">
                        <input type="hidden" id="userId" name="userId">
                        <input type="text" id="username" name="username" placeholder="点击选择用户"
                               class="layui-input" />
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layui-btn-md" lay-submit lay-filter="deptUserRole-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="layui-btn layui-btn-md layui-btn-primary" id="formreset">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
        <table id="deptUserRole-table" lay-filter="deptUserRole-table"></table>
        <script type="text/html" id="deptUserRole-toolbar">
            <button sec:authorize="hasPermission('/sys/deptUserRole/add','sys:deptUserRole:add')"
                    class="layui-btn layui-btn-primary layui-btn-md" lay-event="add">
                <i class="layui-icon layui-icon-add-1"></i>
                新增
            </button>
            <button sec:authorize="hasPermission('/sys/deptUserRole/remove','sys:deptUserRole:del')"
                    class="layui-btn layui-btn-danger layui-btn-md" lay-event="batchRemove">
                <i class="layui-icon layui-icon-delete"></i>
                删除
            </button>
        </script>
        <script type="text/html" id="deptUserRole-bar">
            <button sec:authorize="hasPermission('/sys/deptUserRole/edit','sys:deptUserRole:edit')"
                    class="layui-btn layui-btn-primary layui-btn-sm" lay-event="edit"><i
                    class="layui-icon layui-icon-edit"></i>
            </button>
            <button sec:authorize="hasPermission('/sys/deptUserRole/remove','sys:deptUserRole:del')"
                    class="layui-btn layui-btn-danger layui-btn-sm" lay-event="remove"><i
                    class="layui-icon layui-icon-delete"></i>
            </button>
        </script>
    </div>
</div>
</div>
<th:block th:include="include :: footer"/>
<script>
    let table;
    layui.use(['table', 'form', 'jquery', 'dtree'], function () {
        table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let dtree = layui.dtree;
        let prefix = pName + "/system/deptUserRole/";
        deptUtil.initDeptSelectTree("selectDept", "deptId","deptName", "0")

        $("#roleName").on("click", function () {
            roleUtil.initRoleSelect("roleId", "roleName");
        });

        $("#username").on("click", function () {
            userUtil.initUserSelect("userId", "username");
        });

        let cols = [
            [
                {type: 'checkbox'},
                {
                    field: 'deptName',
                    title: '机构'
                },
                {
                    field: 'roleName',
                    title: '角色'
                },
                {
                    field: 'username',
                    title: '用户'
                },
                {
                    field: 'userSn',
                    title: '工号'
                }
            ]
        ]

        table.render({
            elem: '#deptUserRole-table',
            url: prefix + 'data',
            page: true,
            escape: true,
            cols: cols,
            toolbar: '#deptUserRole-toolbar',
            skin: 'line',
            defaultToolbar: [{
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports']
        });

        table.on('checkbox(deptUserRole-table)', function(obj){

        });
        table.on('row(deptUserRole-table)', function (obj) {

        });
        table.on('tool(deptUserRole-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            }
        });

        table.on('toolbar(deptUserRole-table)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            }
        });

        form.on('submit(deptUserRole-query)', function (data) {
            table.reload('deptUserRole-table', {where: data.field})
            return false;
        });

        window.add = function () {
            let layerIndex = layer.open({
                type: 2,
                title: '新增人员角色授权',
                shade: 0.1,
                maxmin: true,
                area: ['99%', '99%'],
                content: prefix + 'add'
            });
            $(window).on("resize", function () {
                layer.full(layerIndex);
            });
            return false;
        }

        window.edit = function (obj) {
            let layerIndex = layer.open({
                type: 2,
                title: '修改人员角色授权',
                shade: 0.1,
                maxmin: true,
                area: ['99%', '99%'],
                content: prefix + 'edit?id=' + obj.data['id']
            });
            $(window).on("resize", function () {
                layer.full(layerIndex);
            });
            return false;
        }

        window.remove = function (obj) {
            layer.confirm('确定要删除该人员角色授权', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: prefix + "remove/" + obj.data['id'],
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 1) {
                            layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                                obj.del();
                            });
                        } else {
                            layer.alert(result.msg, {icon: 2});
                        }
                    }
                })
            });
        }

        window.batchRemove = function (obj) {
            let data = table.checkStatus(obj.config.id).data;
            if (data.length === 0) {
                layer.msg("未选中数据", {icon: 3, time: 1000});
                return false;
            }
            let ids = "";
            for (let i = 0; i < data.length; i++) {
                ids += data[i].id + ",";
            }
            ids = ids.substr(0, ids.length - 1);
            layer.confirm('确定要删除这些人员角色授权', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: prefix + "batchRemove",
                    dataType: 'json',
                    data: {"ids": ids},
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 1) {
                            layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                                table.reload('deptUserRole-table');
                            });
                        } else {
                            layer.alert(result.msg, {icon: 2});
                        }
                    }
                })
            });
        }

        window.refresh = function (param) {
            table.reload('deptUserRole-table', {where: param});
        }

        $('#formreset').click(function(){
            $('#formId').find('input').val("")
        })
    })
</script>
</body>
</html>